package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultList;
import com.da.lon.wang.xlg.util.ViewHolder;

import java.util.List;

public class SearchListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<SearchResultList> searchResultLists;
    private Context context;

    public SearchListAdapter(List<SearchResultList> list, Context context) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.searchResultLists = list;
    }

    @Override
    public int getCount() {
        return searchResultLists != null ? searchResultLists.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return searchResultLists != null && searchResultLists.size() > position ? searchResultLists
                .get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            SearchResultList searchResultList = searchResultLists.get(position);

            convertView = inflater.inflate(R.layout.ouka_search_listitem, null);

            ImageView imgView = ViewHolder.get(convertView,
                    R.id.search_grid_img);
            LinearLayout xiadan_ll = ViewHolder.get(convertView, R.id.xiadan_ll);
            TextView nameView = ViewHolder.get(convertView,
                    R.id.search_grid_name);
            TextView pricespeView = ViewHolder.get(convertView,
                    R.id.search_grid_pricespe);
            TextView priceView = ViewHolder.get(convertView,
                    R.id.search_grid_price);
            TextView zkView = ViewHolder.get(convertView, R.id.search_grid_zk);
            TextView salesView = ViewHolder.get(convertView, R.id.list_sales);

            MyApplication.loadImageByPicasso(context, searchResultList.getBigimage(), null, imgView, 0, 0, 0);

            nameView.setText(searchResultList.getName());
            pricespeView.setText("￥" + searchResultList.getSeal_price());
            priceView.setText("￥" + searchResultList.getPrice());
            salesView.setText("销量:" + searchResultList.getSales());
            priceView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

            if (searchResultList.getZkl() > 0) {
                zkView.setText(searchResultList.getZkl() + "折");
            } else {
                xiadan_ll.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

}
